//
//  AnimationViewController.m
//  animation1
//
//  Created by 徐国庆 on 15/6/29.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "AnimationHelper.h"

@interface AnimationHelper ()

@end

@implementation AnimationHelper

+ (void)animation:(UIView *)view{
    
    // CGFloat 取值范围 0 - 1 第一个代表阻力  第二个代表速度
    // 参数1 动画持续时间
    // 参数2 延时执行的时间
    // 参数3 阻力
    // 参数4 速度
    // 参数5 样式
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        view.frame = CGRectMake(30, view.frame.origin.y, view.bounds.size.width, view.bounds.size.height);
    } completion:^(BOOL finished) {}];
}

+ (void)animation:(UIView *)view frame:(CGRect)frame{
    
    // CGFloat 取值范围 0 - 1 第一个代表阻力  第二个代表速度
    // 参数1 动画持续时间
    // 参数2 延时执行的时间
    // 参数3 阻力
    // 参数4 速度
    // 参数5 样式
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        view.frame = frame;
        
    } completion:^(BOOL finished) {}];
    
}

+ (void)animation:(UIView *)view delay:(float)delay{
    
    // CGFloat 取值范围 0 - 1 第一个代表阻力  第二个代表速度
    // 参数1 动画持续时间
    // 参数2 延时执行的时间
    // 参数3 阻力
    // 参数4 速度
    // 参数5 样式
    [UIView animateWithDuration:2 delay:delay usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        view.frame = CGRectMake(30, view.frame.origin.y, view.bounds.size.width, view.bounds.size.height);
    } completion:^(BOOL finished) {}];
}

#pragma mark 加载动画
+ (void)popOutsideWithDuration:(NSTimeInterval)duration view:(UIView*)view {
    
    view.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(1.3, 1.3);
        }];
        [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(0.5, 0.5);
        }];
        [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
    } completion:nil];
}

#pragma mark 点击动画
+ (void)popInsideWithDuration:(NSTimeInterval)duration view:(UIView*)view{
    
    view.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(0.8, 0.8);
        }];
        [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
    } completion:nil];
}

+ (void)popInsideMaxWithDuration:(NSTimeInterval)duration view:(UIView*)view{
    
    view.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(0.8, 0.8);
        }];
        [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(1.2, 1.2);
        }];
        
    } completion:nil];
}

+ (void)popInsideMinWithDuration:(NSTimeInterval)duration view:(UIView*)view{
    
    view.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
    
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
            
            view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
        
    } completion:nil];
}

#pragma mark 透明度动画
+(void)opacityAniamtion:(UIView*)view{
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    anima.fromValue = [NSNumber numberWithFloat:0.4f];
    anima.toValue = [NSNumber numberWithFloat:1.0f];
    anima.duration = 0.5f;
    [view.layer addAnimation:anima forKey:@"opacityAniamtion"];
}
@end
